
import * as THREE from 'three';
import { threeExport } from "./threeExport";
import { Raycaster,Vector2 } from 'three';
import { cameraUtil } from './threeCameraUtil';
interface mouseUtils{
    mouse: Vector2,
    mouseClick: Vector2,
    mouseClickTime: number ,
    raycaster: Raycaster | undefined,
}

export const mouseUtil: mouseUtils = {
    mouse:  new THREE.Vector2(),
    mouseClick: new THREE.Vector2(),
    mouseClickTime: 0,
    raycaster:undefined
}
interface buttonUtil {
    buttonLeft: number,
    buttonCenter:number,
    buttonRight: number,
    mouseClickX:number,
    mouseClickY: number,
    mouseClickTime:number
}
export const buttonUtil = <buttonUtil> {
    buttonLeft: 0,
    buttonCenter:1,
    buttonRight: 2,
    mouseClickX:5,
    mouseClickY: 10,
    mouseClickTime:150
}
export function mouseMove(event: MouseEvent) {
    console.log(buttonUtil.mouseClickTime);
    
    if (!cameraUtil.camera) {
        return
    }
    let clientX = event.clientX;
    let clientY = event.clientY;
    let left = threeExport.element.getBoundingClientRect().left;
    let top = threeExport.element.getBoundingClientRect().top;
    let offsetWidth = threeExport.element.offsetWidth;
    let offsetHeight = threeExport.element.offsetHeight;
    mouseUtil.mouse.x = ((clientX - left) / offsetWidth) * 2 - 1;
    mouseUtil.mouse.y = -((clientY - top) / offsetHeight) * 2 + 1;
    document.body.style.cursor = 'default';
    mouseUtil.raycaster = new THREE.Raycaster();
    mouseUtil.raycaster.setFromCamera(mouseUtil.mouse, cameraUtil.camera);
}
export function mouseUp(event: MouseEvent) {
    if (!cameraUtil.camera || !threeExport.scene ) {
        return
    }
    let button = event.button;
    let clientX = event.clientX;
    let clientY = event.clientY;
    let endTime = (Date.now() - mouseUtil.mouseClickTime);
    if (button == buttonUtil.buttonLeft) { 
         if (clientX - mouseUtil.mouseClick.x <= buttonUtil.mouseClickX && clientY - mouseUtil.mouseClick.y <= buttonUtil.mouseClickX && endTime < buttonUtil.mouseClickTime) {
            mouseUtil.raycaster = new THREE.Raycaster();
            mouseUtil.raycaster.setFromCamera(mouseUtil.mouse, cameraUtil.camera);
            let intersects = mouseUtil.raycaster.intersectObjects(threeExport.scene.children);
            if (intersects.length > 0) {
                  //console.log(intersects[0].object.name);
                
            }
        }
    }else if (button == buttonUtil.buttonRight) {
        if (clientX - mouseUtil.mouseClick.x <= buttonUtil.mouseClickX && clientY - mouseUtil.mouseClick.y <= buttonUtil.mouseClickX && endTime < buttonUtil.mouseClickTime) {
            mouseUtil.raycaster = new THREE.Raycaster();
            mouseUtil.raycaster.setFromCamera(mouseUtil.mouse, cameraUtil.camera);
        }
    }
}
export function mouseDown(event:MouseEvent) {
    if (window.event) {
        let clientX = event.clientX;
        let clientY = event.clientY;
        mouseUtil.mouseClick.x = clientX;
        mouseUtil.mouseClick.y = clientY;
        mouseUtil.mouseClickTime = Date.now();
    }
   
}